package cn.com.hhrcw.service.impl;

import cn.com.hhrcw.entity.*;
import cn.com.hhrcw.resumeDeliveryConfig.vo.ResumeInfoVO;
import cn.com.hhrcw.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: recruit-boot-parent
 *
 * @author: JD
 * @create: 2020-11-03 15:56
 **/
@Service
public class EnterpriseService2Impl implements EnterpriseService2 {

    @Autowired
    IPiInfoService piInfoService;

    @Autowired
    ICvResumeService resumeService;

    @Autowired
    private ICvWorkIntentionService cvWorkIntentionService;


    @Autowired
    private ICvUserWorkInfoService cvUserWorkInfoService;


    @Autowired
    private ICvUserEduInfoService cvUserEduInfoService;


    @Autowired
    private ICvProjectInfoService cvProjectInfoService;


    @Autowired
    private ICvTrainService cvTrainService;


    @Autowired
    private ICvLanguageLevelService cvLanguageLevelService;


    @Autowired
    private ICvSkillsService cvSkillsService;


    @Autowired
    private ICvCertificateService cvCertificateService;


    @Autowired
    private IFileService fileService;

    @Autowired
    private ICvFamilyMemberService cvFamilyMemberService;


    @Override
    public ResumeInfoVO getResumeInfo2(String resumeId) {
        ResumeInfoVO result = new ResumeInfoVO();
        LambdaUpdateWrapper<CvResume> wrapper=Wrappers.lambdaUpdate();
        wrapper.eq(CvResume::getId, resumeId)
                .eq(CvResume::getDelFlag, false);

        CvResume cvResume = resumeService.getOne(wrapper);
        if (cvResume == null) {
            return new ResumeInfoVO();
        }
        String userId = cvResume.getUserId();

        LambdaQueryWrapper<PiInfo> infoQuery = new LambdaQueryWrapper<>();
        infoQuery.eq(PiInfo::getDelFlag, false).eq(PiInfo::getUserId, userId);
        PiInfo info = piInfoService.getOne(infoQuery);
        result.setUserInfo(info);
        //基本信息
        result.setResumes(cvResume);

        // 家庭成员
        List<CvFamilyMember> list = cvFamilyMemberService.lambdaQuery().eq(CvFamilyMember::getDelFlag, false)
                .eq(CvFamilyMember::getResumeId, resumeId).list();
        result.setFamilyMembers(list);

        //期望职位
        LambdaQueryWrapper<CvWorkIntention> workIntentionQuery = new LambdaQueryWrapper<>();
        workIntentionQuery.eq(CvWorkIntention::getDelFlag, false).eq(CvWorkIntention::getResumeId, resumeId);
        List<CvWorkIntention> workIntentions = cvWorkIntentionService.list(workIntentionQuery);
        result.setWorkIntentions(workIntentions);

        //工作实习经历
        LambdaQueryWrapper<CvUserWorkInfo> userWorkInfoQuery = new LambdaQueryWrapper<>();
        userWorkInfoQuery.eq(CvUserWorkInfo::getDelFlag, false).eq(CvUserWorkInfo::getResumeId, resumeId);
        List<CvUserWorkInfo> userWorkInfos = cvUserWorkInfoService.list(userWorkInfoQuery);
        result.setUserWorkInfos(userWorkInfos);

        //教育经历
        LambdaQueryWrapper<CvUserEduInfo> userEduInfoQuery = new LambdaQueryWrapper<>();
        userEduInfoQuery.eq(CvUserEduInfo::getDelFlag, false).eq(CvUserEduInfo::getResumeId, resumeId);
        List<CvUserEduInfo> userEduInfos = cvUserEduInfoService.list(userEduInfoQuery);
        result.setUserEduInfos(userEduInfos);

        //项目经历
        LambdaQueryWrapper<CvProjectInfo> projectInfoQuery = new LambdaQueryWrapper<>();
        projectInfoQuery.eq(CvProjectInfo::getDelFlag, false).eq(CvProjectInfo::getResumeId, resumeId);
        List<CvProjectInfo> projectInfos = cvProjectInfoService.list(projectInfoQuery);
        result.setProjectInfos(projectInfos);

        //培训经历
        LambdaQueryWrapper<CvTrain> trainQuery = new LambdaQueryWrapper<>();
        trainQuery.eq(CvTrain::getDelFlag, false).eq(CvTrain::getResumeId, resumeId);
        List<CvTrain> trainInfos = cvTrainService.list(trainQuery);
        result.setTrainInfos(trainInfos);

        //语言水平
        LambdaQueryWrapper<CvLanguageLevel> languageLevelQuery = new LambdaQueryWrapper<>();
        languageLevelQuery.eq(CvLanguageLevel::getDelFlag, false).eq(CvLanguageLevel::getResumeId, resumeId);
        List<CvLanguageLevel> languageLevelInfos = cvLanguageLevelService.list(languageLevelQuery);
        result.setLanguageLevelInfos(languageLevelInfos);

        //职业特长与技能（专业技能）
        LambdaQueryWrapper<CvSkills> skillsQuery = new LambdaQueryWrapper<>();
        skillsQuery.eq(CvSkills::getDelFlag, false).eq(CvSkills::getResumeId, resumeId);
        List<CvSkills> skillsInfos = cvSkillsService.list(skillsQuery);
        result.setSkillsInfos(skillsInfos);

        //资质证书
        LambdaQueryWrapper<CvCertificate> certificateQuery = new LambdaQueryWrapper<>();
        certificateQuery.eq(CvCertificate::getDelFlag, false).eq(CvCertificate::getResumeId, resumeId);
        List<CvCertificate> certificateInfos = cvCertificateService.list(certificateQuery);
        result.setCertificateInfos(certificateInfos);
        List<File> files=new ArrayList<>();
        if (StringUtils.isNotBlank(cvResume.getEnclosureId())){
            String[] strings = cvResume.getEnclosureId().split(",");
            for (String string : strings) {
                File file = fileService.getById(string);
                if (null != file){
                    files.add(file);
                }
            }
        }
        result.setFiles(files);
        return result;
    }


}
